package service.network
{
	import flash.events.EventDispatcher;
	
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.HTTPService;
	
	import service.events.NetworkDataEvent;
	import service.interfaces.INetworkCommunication;

	public class HTTPNetwork extends EventDispatcher implements INetworkCommunication
	{
		private var webService:HTTPService;
		
		public function HTTPNetwork() {
			webService = new HTTPService();
			webService.method = "POST";
			webService.contentType = "application/xml";
			webService.resultFormat = "xml";
			
			webService.addEventListener(ResultEvent.RESULT, onResult);
			webService.addEventListener(FaultEvent.FAULT, onFault);
		}
		
		public function send(xml:XML, appUrl:String):void {
			webService.url = appUrl;
			webService.request = xml;
			webService.send();
		}
		
		private function onResult(event:ResultEvent):void {
			dispatchEvent(new NetworkDataEvent(new XML(event.result)));
		}
		
		private function onFault(event:FaultEvent):void {
			dispatchEvent(event);
		}
	}
}